package com.xyy.saas.payment.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @author wangtianqi
 * @date 2019/11/20 13:30
 */
public class DateUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);

    public static final DateTimeFormatter PATTERN_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter PATTERN_DATE2 = DateTimeFormatter.ofPattern("yyyyMMdd");

    /**
     * 日期格式 年 如2009
     */
    public static final String DATEFORMATYEAR = "yyyy";

    /**
     * 日期格式 年 月  如 2009-02
     */
    public static final String DATEFORMATMONTH = "yyyy-MM";

    /**
     * 日期格式 年 月 日 如2009-02-26
     */
    public static final String DATEFORMATDAY = "yyyy-MM-dd";

    /**
     * 日期格式 年 月 日 如2009-02-26
     */
    public static final String DATEFORMATDAY_02 = "yyyyMMdd";

    /**
     * 日期格式 年 月 日 如20090226120000
     */
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    /**
     * 日期格式 年 月 日 时 如2009-02-26 15
     */
    public static final String DATEFORMATHOUR = "yyyy-MM-dd HH";

    /**
     * 日期格式 年 月 日 时 分 如2009-02-26 15:40
     */
    public static final String DATEFORMATMINUTE = "yyyy-MM-dd HH:mm";

    /**
     * 日期格式年 月 日 时 分 秒 如 2009-02-26 15:40:00
     */
    public static final String DATEFORMATSECOND = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式年 月 日 时 分 秒 毫秒 如2009-02-26 15:40:00 110
     */
    public static final String DATEFORMATMILLISECOND = "yyyy-MM-dd HH:mm:ss SSS";
    /**
     * 零点
     */
    public static final String ZERO = "00:00:00";
    /**
     * 九点
     */
    public static final String NINE = "09:00:00";

    /**
     * 周
     */
    public static final String W7 = "周天";
    public static final String W1 = "周一";
    public static final String W2 = "周二";
    public static final String W3 = "周三";
    public static final String W4 = "周四";
    public static final String W5 = "周五";
    public static final String W6 = "周六";

    private static final String[] weekDays = {W7, W1, W2, W3, W4, W5, W6};

    public static String localDateTimeFormat(LocalDateTime localDateTime) {
        return localDateTime.format(PATTERN_DATE2);
    }
    
    public static LocalDateTime toLocalDateTime(String str) {
        return LocalDateTime.parse(str, PATTERN_DATE);
    }

    public static boolean isAtLeastOneYearApart(Date date) {
        try {
            LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate oneYearLater = localDate.plusYears(1);
            LocalDate currentDate = LocalDate.now();
            //  检查当前日期是否在一年后的日期之后或相等
            return currentDate.isEqual(oneYearLater) || currentDate.isAfter(oneYearLater);
        } catch (Exception e) {
            LOGGER.error("DateUtil#isOneYearBefore error", e);
            return false;
        }
    }

    public static boolean isMoreThanSixMonthsAgo(Date date) {
        try {
            // 转换传入的 Date 为 LocalDate
            LocalDate inputDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            // 获取当前日期
            LocalDate currentDate = LocalDate.now();

            // 计算日期差值
            long monthsBetween = ChronoUnit.MONTHS.between(inputDate, currentDate);

            // 如果差值大于等于 6 个月，返回 true
            return monthsBetween >= 6;
        } catch (Exception e) {
            LOGGER.error("DateUtil#isMoreThanSixMonthsAgo error", e);
            return false;
        }
    }

    public static Date convertLocalDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Long toMilli(String date) {
        try {
            return LocalDateTime.parse(date, PATTERN_DATE).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        } catch (Exception e) {
            LOGGER.error("DateUtil#toMilli error date:{}", date);
            return null;
        }
    }

    public static Date sdf1ToDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(date);
        } catch (ParseException e) {
            LOGGER.error("DateUtil#sdf1ToDate error date:{}", date);
            return null;
        }
    }

    /**
     * 获取当前时间
     *
     * @return Date类型
     */
    public static Date now() {
        return new Date();
    }

    /**
     * unix 秒 转 date
     *
     * @param unix
     * @return
     */
    public static Date unixToDate(Long unix) {
        if (unix == null) {
            return null;
        }

        try {
            return new Date(unix * 1000);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
            return null;
        }
    }

    /**
     * stringDate 转 unix
     *
     * @param dateTime
     * @param format
     * @return
     */
    public static Long dateToUnix(String dateTime, String format) {
        if (StringUtils.isEmpty(dateTime) || StringUtils.isEmpty(format)) {
            return null;
        }

        Date date = parase(dateTime, format);

        if (date == null) {
            return null;
        }

        return date.getTime() / 1000;
    }

    /**
     * 毫秒 转 date
     *
     * @return
     */
    public static Date timeToDate(Long time) {
        if (time == null) {
            return null;
        }

        try {
            return new Date(time);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
            return null;
        }
    }

    /**
     * 按指定的格式，把Date转换成String 如date为null,返回null
     *
     * @param date   Date参数
     * @param format 日期格式
     * @return String
     */
    public static String format(Date date, String format) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 按指定的格式，把string转换成Date 如string为空或null，返回null
     *
     * @param string
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date parase(String string, String format) {
        if (StringUtils.isEmpty(string)) {
            return null;
        }

        try {
            return new SimpleDateFormat(format).parse(string);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
            return null;
        }
    }

    public static Long strToLong(String date) {
        try {
            Date result = parase(date, DATEFORMATSECOND);
            return dateToLong(result);
        } catch (Exception ex) {
            return null;
        }
    }

    public static Long strToLong(String date, String format) {
        try {
            Date result = parase(date, format);
            return dateToLong(result);
        } catch (Exception ex) {
            return null;
        }
    }

    public static Long dateToLong(Date date) {
        try {
            return date == null ? null : date.getTime();
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 按年月日格式，把String转换成Date 如果String为空或者null，返回null
     *
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static Date string2Date(String dateString) throws ParseException {
        Date dateTime = null;
        if (!StringUtils.isEmpty(dateString)) { //如果string时间参数不是空
            final SimpleDateFormat df = new SimpleDateFormat(DATEFORMATDAY); //年月日时间格式化
            Date date = null;
            date = df.parse(dateString); //String转换Date
            dateTime = new Date(date.getTime());
        }
        return dateTime;
    }

    /**
     * 获取当前系统时间
     *
     * @return
     */
    public static Date getSysDate() {
        Calendar calender = Calendar.getInstance();
        return calender.getTime();
    }

    /**
     * 取一天的开始时间 精确到秒 如果date为null，返回null
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static String getDayFirstSecond(Date date) {
        if (date == null) {
            return null;
        }
        String str = format(date, DATEFORMATDAY) + " 00:00:00";
        return str;
    }

    /**
     * 取一天的开始时间 精确到秒 如果string为""，返回null
     *
     * @return
     * @throws Exception
     */
    public static String getDayFirstSecond(String date) {
        if (date.equals("")) {
            return null;
        }
        String ret = "";
        try {
            ret = getDayFirstSecond(string2Date(date));
        } catch (ParseException e) {
        }
        return ret;
    }

    /**
     * 取一天的结束时间 精确到秒 如果date为null，返回null
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static String getDayLastSecond(Date date) {
        if (date == null) {
            return null;
        }
        final String str = format(date, DATEFORMATDAY) + " 23:59:59";
        return str;
    }

    /**
     * 取一天的结束时间 精确到秒 如果string为""，返回null
     *
     * @return
     * @throws Exception
     */
    public static String getDayLastSecond(String date) {
        if (date.equals("")) {
            return null;
        }
        String ret = "";
        try {
            ret = getDayLastSecond(string2Date(date));
        } catch (ParseException e) {
        }
        return ret;
    }

    /**
     * 取一天的开始时间 精确到毫秒
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static Date getDayFirstTime(Date date) throws Exception {
        if (date == null) {
            return null;
        }
        final String str = format(date, DATEFORMATDAY) + " 00:00:00 000";
        return parase(str, DATEFORMATMILLISECOND);
    }

    /**
     * 取一天的结束时间 精确到毫秒
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static Date getDayLastTime(java.util.Date date) throws Exception {
        if (date == null) {
            return null;
        }
        final String str = format(date, DATEFORMATDAY) + " 23:59:59 999";
        return parase(str, DATEFORMATMILLISECOND);
    }

    /**
     * 获取昨天的日期
     *
     * @param strDate
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getYestoday(String strDate) throws ParseException {
        if (null != strDate && strDate.length() > 0) {
            final GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(parase(strDate, DATEFORMATDAY));
            cal.add(Calendar.DAY_OF_MONTH, -1);
            final String str = format(cal.getTime(), DATEFORMATDAY);
            return parase(str, DATEFORMATDAY);
        }
        return null;
    }

    /**
     * 获取明天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getTomorrow() throws ParseException {
        final GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(getSysDate());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        final String str = format(cal.getTime(), DATEFORMATSECOND);
        return parase(str, DATEFORMATSECOND);
    }

    /**
     * 获取指定日期下一天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getNextDay(Date someDate) throws ParseException {
        final Calendar ca = Calendar.getInstance();
        ca.setTime(someDate);
        ca.add(Calendar.DAY_OF_MONTH, 1);
        final String str = format(ca.getTime(), DATEFORMATDAY);
        return parase(str, DATEFORMATDAY);
    }

    /**
     * 根据当前日期返回本月的最后一天
     *
     * @param someDate
     * @return
     */
    public static Date getLastDayOfMonth(Date someDate) {
        final Calendar ca = Calendar.getInstance();
        ca.setTime(someDate); // someDate 为你要获取的那个月的时间
        ca.set(Calendar.DAY_OF_MONTH, 1);
        ca.add(Calendar.MONTH, 1);
        ca.add(Calendar.DAY_OF_MONTH, -1);
        return ca.getTime();
    }

    /**
     * 得到本月最后一天的日期
     */
    public static Date getLastDayOfMonth(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = sdf.parse(dateStr);
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.DAY_OF_MONTH, 1);
        ca.add(Calendar.MONTH, 1);
        ca.add(Calendar.DAY_OF_MONTH, -1);
        return ca.getTime();
    }

    /**
     * 当前日期 yyyy-MM-dd
     *
     * @return
     */
    public static String getToday() {
        Calendar ca = Calendar.getInstance();
        return format(ca.getTime(), DATEFORMATDAY);
    }

    /**
     * 当前日期上个月 yyyy-MM-dd
     *
     * @return
     */
    public static String getLastMonthToday() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.MONTH, ca.get(Calendar.MONTH) - 1);
        return format(ca.getTime(), DATEFORMATDAY);
    }

    /**
     * 当前日期上个星期yyyy-MM-dd
     *
     * @return
     */
    public static String getLastWeekToday() {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DAY_OF_MONTH, -7);
        String str = format(ca.getTime(), DATEFORMATDAY);
        return str;
    }

    /**
     * 当前日期 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getTodayToSecond() {
        Calendar ca = Calendar.getInstance();
        String str = format(ca.getTime(), DATEFORMATSECOND);
        return str;
    }

    /**
     * 当前日期-月 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getLastMonthTodayToSecond() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.MONTH, ca.get(Calendar.MONTH) - 1);
        String str = format(ca.getTime(), DATEFORMATSECOND);
        return str;
    }

    /**
     * 当前日期-一周 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getLastWeekTodayToSecond() {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DAY_OF_MONTH, -7);
        String str = format(ca.getTime(), DATEFORMATSECOND);
        return str;
    }

    /**
     * 得到本月第一天的日期
     */
    public static Date getStartDayOfMonth(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_MONTH, 1);
        return cDay.getTime();
    }

    /**
     * 得到本月第一天的日期
     */
    public static Date getStartDayOfMonth(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = sdf.parse(dateStr);
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_MONTH, 1);
        return cDay.getTime();
    }

    /**
     * 得到本月最后一天的日期
     */
    public static Date getEndDayOfMonth(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cDay.getTime();
    }

    /**
     * 得到本月最后一天的日期
     */
    public static Date getEndDayOfMonth(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = sdf.parse(dateStr);
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cDay.getTime();
    }

    /**
     * 得到下个月第一天的日期
     */
    public static Date getStartDayOfNextMonth(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.add(Calendar.MONTH, +1);
        cDay.set(Calendar.DAY_OF_MONTH, 1);
        return cDay.getTime();
    }

    /**
     * 得到下个月第一天的日期
     */
    public static Date getStartDayOfNextMonth(String dateStr) throws ParseException {
        Date date = parase(dateStr, DATEFORMATMONTH);
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.add(Calendar.MONTH, +1);
        cDay.set(Calendar.DAY_OF_MONTH, 1);
        return cDay.getTime();
    }

    /**
     * 获取指定日期所在周的周一
     */
    public static Date getMonday(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_WEEK, 2);// 老外将周日定位第一天，周一取第二天
        return cDay.getTime();
    }

    /**
     * 获取指定日期所在周日
     */
    public static Date getSunday(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        if (Calendar.DAY_OF_WEEK == cDay.getFirstDayOfWeek()) { // 如果刚好是周日，直接返回
            return date;
        } else {// 如果不是周日，加一周计算
            cDay.add(Calendar.DAY_OF_YEAR, 7);
            cDay.set(Calendar.DAY_OF_WEEK, 1);
            return cDay.getTime();
        }
    }

    /**
     * 获取本年的第一天
     */
    public static Date getFirstDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    /**
     * 获取本年的第一天
     */
    public static Date getFirstDayOfYear(String dateStr) throws ParseException {
        Date date = parase(dateStr, DATEFORMATYEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    /**
     * 获取本年的最后一天
     */
    public static Date getLastDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return calendar.getTime();
    }

    /**
     * 获取本年的最后一天
     */
    public static Date getLastDayOfYear(String dateStr) throws ParseException {
        Date date = parase(dateStr, DATEFORMATYEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return calendar.getTime();
    }

    /**
     * 获取下一年的第一天
     */
    public static Date getFirstDayOfNextYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, +1);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    /**
     * 获取下一年的第一天
     */
    public static Date getFirstDayOfNextYear(String dateStr) throws ParseException {
        Date date = parase(dateStr, DATEFORMATYEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, +1);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    /**
     * 获取昨天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getDay(int offset) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, offset);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取当前时间分钟之前 精确到分钟
     */
    public static String getBeforMinutesSysDate(int minute) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -minute);
        return format(calendar.getTime(), DATEFORMATMINUTE);
    }

    /**
     * 获取当前时间分钟之前 精确到分钟
     */
    public static String getMinuteSysDate() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        return format(calendar.getTime(), DATEFORMATMINUTE);
    }


    /**
     * 获取当前日期前后N天日期
     *
     * @return
     * @throws ParseException
     */
    public static Date getOtherDay(int n) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, n);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        return calendar.getTime();
    }

    /**
     * 获取指定日期前后N天日期
     *
     * @param coordinate
     * @param offset
     * @return
     */
    public static Date getOtherDay(Date coordinate, int offset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(coordinate);
        calendar.add(Calendar.DATE, offset);
        return calendar.getTime();
    }

    /**
     * 计算目标日期到当前日期之间有多少秒
     *
     * @param target
     * @return
     */
    public static Long getSecondsFromToday(Date target) {
        Date today = new Date();
        Long diff = target.getTime() - today.getTime();
        return (diff / 1000);
    }

    /**
     * 获取几天&几分钟后的日期，负数为往前
     *
     * @param day
     * @param hour
     * @return
     */
    public static Date getDayAfterToday(int day, int hour) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, day);
        c.add(Calendar.HOUR, hour);
        return c.getTime();
    }


    /**
     * @param timestamp
     * @param pattern
     * @return
     * @author wangqinghua 2014-12-3
     */
    public static String timestamp2String(Timestamp timestamp, String pattern) {
        if (timestamp == null) {
            return "";
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(pattern)) {
            pattern = YYYYMMDDHHMMSS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date(timestamp.getTime()));
    }

    /**
     * @return
     * @author wangqinghua 2014-12-3
     */
    public static Timestamp currentTimestamp() {
        return new Timestamp(new Date().getTime());
    }

    /**
     * @param pattern
     * @return
     * @author wangqinghua 2014-12-3
     */
    public static String currentTimestamp2String(String pattern) {
        return timestamp2String(currentTimestamp(), pattern);
    }


    /**
     * 日期转换为字符串
     *
     * @param date
     * @param pattern
     * @return
     * @author wangqinghua 2014-12-3
     */
    public static String date2String(Date date, String pattern) {
        if (date == null)
            return "";
        if (org.apache.commons.lang3.StringUtils.isEmpty(pattern)) {
            pattern = YYYYMMDDHHMMSS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.CHINESE);
        return sdf.format(date);
    }

    public static String currDateStr() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS, Locale.CHINESE);
        return sdf.format(new Date());
    }

    /**
     * 获取中文星期几
     *
     * @param date
     * @return
     */
    public static String getWeek(Date date) {
        try {
            //第二个参数切记加上，否则linux系统显示的是英文
            SimpleDateFormat sdf = new SimpleDateFormat("EEEE", Locale.SIMPLIFIED_CHINESE);
            String week = sdf.format(date);
            return week;
        } catch (Exception e) {
            LOGGER.error("DateUtils.getWeek#date:{}", date, e);
            return org.apache.commons.lang3.StringUtils.EMPTY;
        }
    }

    /**
     * 获取两个时间之间的所有天维度的时间
     *
     * @param start
     * @param end
     * @param format
     * @return
     */
    public static List<String> getBetweenDates(Date start, Date end, String format) {
        List<String> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        Collections.reverse(result);
        return result;
    }

    /**
     * 判断target是否在start和end之间
     *
     * @param target
     * @param start
     * @param end
     * @return
     */
    public static boolean isBetween(Date target, Date start, Date end) {
        try {
            return target.getTime() >= start.getTime() && target.getTime() <= end.getTime();
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }

        return false;
    }

    /**
     * 获取制定时间是周几
     *
     * @param date
     * @return
     */
    public static String getWeekDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDays[Math.max(w, 0)];
    }

    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    public static Date getNextWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 7);
        return cal.getTime();
    }

    /**
     * 获取两个日期的分钟差
     */
    public static long getTimeDiffMinute(Date start, Date end) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(start);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(end);
        return (cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 60000;
    }

    /**
     * 获取事件差
     *
     * @param start
     * @param end
     * @return
     */
    public static long getTimeDiff(Date start, Date end) {
        try {
            if (start == null || end == null) {
                return 0;
            }

            return end.getTime() - start.getTime();
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
            return 0;
        }
    }

    public static String connect(String date, String time) {
        return date + " " + time;
    }

    /**
     * 计算N月之前数据
     */
    public static String difMonthPingan(Date startData, int difMonth) {
        LocalDateTime localDateTime = startData.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusMonths(difMonth * -1);
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return format(date, DateUtil.DATEFORMATMONTH + "-01");
    }

    /**
     * 获取两个时间之间的月份数据
     */
    public static List<String> getBetweenMonths(String start, String end, String format) throws ParseException {
        List<String> result = new ArrayList<>();
        if (StringUtils.isEmpty(start) || StringUtils.isEmpty(end)) {
            return result;
        }
        result.add(start);
        if (start.compareTo(end) >= 0) {
            return result;
        }
        while (start.compareTo(end) < 0) {
            start = nextMonth(start + "-01", format);
            result.add(start);
        }
        return result;
    }

    /**
     * 计算下个月日期
     */
    public static String nextMonth(String currentDate, String format) throws ParseException {
        Date startData = new SimpleDateFormat("yyyy-MM-dd").parse(currentDate);
        LocalDateTime localDateTime = startData.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime()
                .plusMonths(1);
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return format(date, format);
    }


}
